package com.tianqu.tms.www.proxymember.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.entity.ProxyCarInfo;
import com.tianqu.tms.entity.User;
import com.tianqu.tms.www.proxymember.action.ProxyMemberCarListInputAction;
import com.tianqu.tms.www.proxymember.viewhelper.ProxyMemberCarSearchInputViewHelper;

/**
 * @author zhangxueliang
 * 车辆搜索录入
 */
public class ProxyMemberCarListInputController extends TmsController{
	private Logger logger = Logger
	.getLogger(ProxyMemberCarListInputController.class);
	public static final String PAGING = "paging";
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		try{
			controller.begin();

			ProxyMemberCarSearchInputViewHelper viewhelp = new ProxyMemberCarSearchInputViewHelper();
			viewhelp = (ProxyMemberCarSearchInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
			String serial = "";
			String licensePlate = "";
			String timeA = "";
			String timeB = "";
			int status =0;
			String memberId = "";
			serial = viewhelp.getSerial();
			licensePlate = viewhelp.getLicensePlate();
			status = Integer.parseInt(viewhelp.getStatus());
			timeA = viewhelp.getTimeA();
			timeB = viewhelp.getTimeB();
	
			TmsUserProfile profile = uc.getUserProfile();
			User user = User.getInstance();
			user = user.getUserInfoById(profile.getUniqueId());
			Member mem = Member.getInstance();//根据当前登入的userId 载入商户id
			if(user.getId() > 0){
				mem = mem.findMemberInfoByUserId(user.getLoginId());
				if(mem.getMemId() > 0){
					memberId = String.valueOf(mem.getMemId());
				}
			}
			
			ProxyCarInfo proxyCarInfo = ProxyCarInfo.getInstance();//如果memberId为空直接返回空的list
			
			if (uc.getAttribute(ProxyMemberCarListInputAction.PAGENO) != null) {
				proxyCarInfo.setPageNo(Integer.parseInt((String) uc
						.getAttribute(ProxyMemberCarListInputAction.PAGENO)));
			}
			if (uc.getAttribute(ProxyMemberCarListInputAction.MAXINDEX) != null) {
				proxyCarInfo.setMaxIndex(Integer.parseInt((String) uc
						.getAttribute(ProxyMemberCarListInputAction.MAXINDEX)));
			}
			if (uc.getAttribute(ProxyMemberCarListInputAction.SORTKEY) != null) {
				proxyCarInfo.setSortKey(Integer.parseInt((String) uc
						.getAttribute(ProxyMemberCarListInputAction.SORTKEY)));
			}
			if (uc.getAttribute(ProxyMemberCarListInputAction.ASC) != null) {
				proxyCarInfo.setAsc((new Boolean((String) uc
						.getAttribute(ProxyMemberCarListInputAction.ASC)))
						.booleanValue());
			}
			List<ProxyCarInfo> proxyCarInfoList = new ArrayList<ProxyCarInfo>();
			if(!"".equals(memberId) && memberId != null){
				proxyCarInfoList = proxyCarInfo.findAllProxyCar(memberId, serial, licensePlate, timeA, status, timeB);
			}else{
				proxyCarInfoList = null;
			}
			if(proxyCarInfoList.size() > 0 && proxyCarInfoList != null){
				viewhelp.setProxyCarList(proxyCarInfoList);
			}

			uc.setAttribute(TmsConstants.VIEWHELP, viewhelp);
			uc.setAttribute(PAGING, proxyCarInfo);
			controller.commit();
		}catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		logger.debug(this.getClass().getName() + "#execute was completed.");
		
	}

}
